package com.ayu.commentbackend.service.impl;
import com.ayu.commentbackend.StatusConstant;
import com.ayu.commentbackend.mapper.BlogMapper;
import com.ayu.commentbackend.pojo.dto.BlogAddDTO;
import com.ayu.commentbackend.pojo.dto.BlogPageQueryDTO;
import com.ayu.commentbackend.pojo.dto.BlogUpdateDTO;
import com.ayu.commentbackend.pojo.entity.Blog;
import com.ayu.commentbackend.pojo.result.PageResult;
import com.ayu.commentbackend.service.BlogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Slf4j
public class BlogServiceImpl implements BlogService {
    @Autowired
    BlogMapper blogMapper;

    @Override
    public PageResult pageQuery(BlogPageQueryDTO blogPageQueryDTO) {
        PageHelper.startPage(blogPageQueryDTO.getPageNum(), blogPageQueryDTO.getPageSize());
        Page<Blog> pages = blogMapper.pageQuery(blogPageQueryDTO);

        PageResult pageResult = new PageResult();
        pageResult.setTotal(pages.getTotal());
        pageResult.setRecords(pages.getResult());

        return pageResult;
    }

    @Override
    public Long add(BlogAddDTO blogAddDTO) {
        Blog blog = new Blog();
        BeanUtils.copyProperties(blogAddDTO, blog);
        blog.setCreateTime(LocalDateTime.now());
        blog.setUpdateTime(LocalDateTime.now());

        // TODO 从jwt获取id，查询username并插入

        blogMapper.insert(blog);
        return blog.getId();
    }

    @Override
    public void update(BlogUpdateDTO blogUpdateDTO) {
        Blog blog = new Blog();
        BeanUtils.copyProperties(blogUpdateDTO, blog);
        blog.setUpdateTime(LocalDateTime.now());

        blogMapper.update(blog);
    }

    @Override
    public void updateAiStatus(int aiStatus, Long id) {
        blogMapper.updateAiStatus(aiStatus, id);
    }

    @Override
    public void updateOtherEventWithAiStatus(String aiResult, int aiStatus, Long id) {
        blogMapper.updateOtherEventWithAiStatus(aiResult, aiStatus, StatusConstant.AFTER_AI, id);
    }

    @Override
    public Blog detail(Integer id) {
        return blogMapper.selectById(id);
    }

    @Override
    public List<Blog> getByUserId(Integer createUser) {
        return blogMapper.selectByUserId(createUser);
    }

    @Override
    public void delete(Integer id) {
        blogMapper.delete(id);
    }
}
